<?php
class Questions extends Public_Controller
{
	function __construct()
	{
		parent::__construct();
	}
	
	function index($id = NULL)
	{
		$data['category'] = new Question_Category();
		$data['category']->where('active',1)->get();
		if($data['category']->exists())
		{
			$this->template->build('question_index',$data);	
		}
		else
		{
			set_notify('error','ยังไม่มีคำถามชิงรางวัลในขณะนี้ค่ะ');
			redirect($_SERVER['HTTP_REFERER']);
		}
	}
	
	function save()
	{
		if($_POST)
		{
			$email = new Answer_email;
			$email->email = $_POST['email'];
			$email->question_category_id = $_POST['question_category_id'];
			$email->save();
			foreach($_POST['answer'] as $key => $value)
			{
				if($value)
				{
					$answer = new Answer;
					$answer->answer = $value;
					$answer->question_id = $_POST['question_id'][$key];
					$answer->answer_email_id = $email->id;
					$answer->save(); 
				}
			}
			redirect('questions/thankyou');
		}
	}
	
	function thankyou()
	{
		$this->template->build('question_thankyou');
	}

}
?>